#include <bridget_communicator.h>
#include <ADCM_Domain.h>
#include <vector>
#include <string>
#include <WMI_Reader.h>
#include <cm_dblitemanager.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include "unit--.h"

using namespace std;
using namespace unit_minus;

wstring domain;
wstring fullyQualifiedDomain;
wstring user;
wstring password;
wstring host;

vector<string> computersList;
vector<string> fileList;
vector<string> registryList;
vector<string> processList;
vector<string> driveList;

int* active;


void
getCommands(const wstring& input, wstring& command, wstring& result)
{     
    int name = input.rfind(L"=", input.size() - 1);
    result = input.substr(name + 1);
      command = input.substr(0,name);     
}

void 
openConfig()
{
      if(active != NULL)
      {           
            return;
      }

      active=new int(1);

      bool band=true;

      ifstream f("TestConfig.txt");

    string buffer;
    while (!f.eof())
    {
        getline(f, buffer);
            wstring temp(buffer.length(),L' ');
            copy(buffer.begin(), buffer.end(), temp.begin());
            
            if(temp.size() >= 2)
            {
                  if(temp[0] !=  BACKSLASH && temp[1] != BACKSLASH)
                  {
                        wstring item, result;
                        getCommands(temp, item, result);

                string res(result.begin(), result.end());

                        if(item.compare(L"computer") == 0)
                        {
                              computersList.push_back(res); 
                        }

                        /*else if(item.compare(L"registryPath") == 0)
                        {
                              registryList.push_back(res);
                        }

                        else if(item.compare(L"file") == 0)
                        {
                              fileList.push_back(res);
                        }

                        else if(item.compare(L"process") == 0)
                        {
                              processList.push_back(res);
                        }

                        else if(item.compare(L"drive") == 0)
                        {
                              driveList.push_back(res);
                        }*/

                        if(item.compare(L"domain") == 0)
                        {
                              domain = result;
                        }

                        else if(item.compare(L"fqDomain") == 0)
                        {
                              fullyQualifiedDomain = result;
                        }

                        else if(item.compare(L"user") == 0)
                        {
                              user = result;
                        }

                        else if(item.compare(L"password") == 0)
                        {
                              password = result;
                        }

                        else if(item.compare(L"host") == 0)
                        {
                              host = result;
                        }

                        else if(item.compare(L"user") == 0)
                        {
                              user = result;
                        }

                        else if(item.compare(L"password") == 0)
                        {
                              password = result;
                        }
                        else if(item.compare(L"host") == 0)
                        {
                              host = result;
                        }     
                  }
            }     
    }
    f.close();

      wcout<<"user = "<<user<<endl;
      wcout<<"pass = "<<password<<endl;
      wcout<<"host = "<<host<<endl;
      wcout<<"domain = "<<domain<<endl;
      wcout<<"fqdn = "<<fullyQualifiedDomain<<endl;

      system("PAUSE");
}


testSuite(BCommunicatorSuite);

testCase(getADComputers, BCommunicatorSuite)
{
	cout << "---------- Start getADComputers Test ----------" << endl;
	
	openConfig();
	vector<ADCM_Computer> computers;

	WMI_HResult whres;
	whres = bridget_communicator::getComputers(domain, fullyQualifiedDomain, wstring(L""), user, password, computers);

	if(FAILED(whres.getHRESULT()))
	{
		cout << "Connection not stablished" << endl;
	}

	int computersCount = 0;  
	for(int i = 0; i < computers.size(); i++)
	{
		for(int j = 0; j < computersList.size(); j++)
		{
			  wstring computerName(computersList[j].begin(), computersList[j].end());
			  if(computers[i].getCn().compare(computerName) == 0)
			  {
					assertTrue(computerName.compare(computers[i].getCn()) == 0);
					computersCount++;
			  }
		}
	}

	assertTrue(computersCount == computersList.size());

	cout << "---------- End getADComputers Test ----------" << endl << endl;
}


testCase(getComputerResult, BCommunicatorSuite)
{
	cout << "---------- Start getComputersResult Test ----------" << endl;

	openConfig();

	vector<ADCM_Computer> computers;

	WMI_HResult whres;
	whres = bridget_communicator::getComputers(domain, fullyQualifiedDomain, wstring(L""), user, password, computers);

	if(FAILED(whres.getHRESULT()))
	{
		cout << "Connection not stablished" << endl;
	}

	CM_ResultCollection resultCollection;
	bridget_communicator::getComputersResult(computers[0].getIdComputer(), resultCollection, user, 
										   password, host, fullyQualifiedDomain, false);

	assertTrue(resultCollection.getProcesses().getObjects().size() > 0);

	assertTrue(resultCollection.getShares().getObjects().size() > 0);

	assertTrue(resultCollection.getRegistries().getObjects().size() > 0); 

	assertTrue(resultCollection.getFileDirs().getObjects()->getContent().size() > 0); 

	cout << "---------- End getComputersResult Test ----------" << endl << endl;
}


testCase(saveComputerResult, BCommunicatorSuite)
{
	cout << "---------- Start saveComputersResult Test ----------" << endl;

	openConfig();
	vector<ADCM_Computer> computers;
	
	WMI_HResult whres;
	whres = bridget_communicator::getComputers(fullyQualifiedDomain,computers);

	if(FAILED(whres.getHRESULT()))
	{
		cout << "Connection not stablished" << endl;
	}
	if(computers.size() > 0)
	{
		CM_ResultCollection resultCollection;
		bridget_communicator::getComputersResult(computers[0].getIdComputer(), resultCollection, user, 
											   password, host, fullyQualifiedDomain, false);

		bridget_communicator::save(computers[0].getIdComputer(), resultCollection);
	}
	else
	{
		wcout << "No computers found in the domain " << fullyQualifiedDomain << endl;
	}

	CM_ResultCollection differenceCollection;
	bridget_communicator::getDifferences(computers[0].getIdComputer(), differenceCollection);

	cout<<differenceCollection.getFileDirs().getObjects()->getContent().size()<<endl;
	cout<<differenceCollection.getProcesses().getObjects().size()<<endl;
	cout<<differenceCollection.getRegistries().getObjects().size()<<endl;
	cout<<differenceCollection.getShares().getObjects().size()<<endl;

	system("pause");
}


testCase(saveComputers, BCommunicatorSuite)
{
	cout << "---------- Start saveComputers Test ----------" << endl;

	openConfig();
	vector<ADCM_Computer> computers;
	vector<ADCM_Computer> computersDB;

	WMI_HResult whres;
	whres = bridget_communicator::getComputers(domain, fullyQualifiedDomain, wstring(L""), user, password, computers);

	if(FAILED(whres.getHRESULT()))
	{
		cout << "Connection not stablished" << endl;
	}

	bridget_communicator::save(domain, fullyQualifiedDomain, computers);

	bridget_communicator::getComputers(fullyQualifiedDomain, computersDB);

	assertTrue(computers.size() == computersDB.size());

	int computersCount = 0;
	for(int i = 0; i < computers.size(); i++)
	{
		for(int j = 0; j < computersDB.size(); j++)
		{
			if(computers[i].getCn().compare(computersDB[j].getCn()) == 0)
			{
				computersCount++;
			}
		}
	}

	assertTrue(computersCount == computers.size());

	cout << "---------- End saveComputers Test ----------" << endl << endl;
}
